package com.yyk.yiliao.ui.trade;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.View;

import com.orhanobut.logger.Logger;
import com.yyk.yiliao.R;
import com.yyk.yiliao.base.BaseActivity;
import com.yyk.yiliao.base.BaseRecyclerAdapter;
import com.yyk.yiliao.base.BaseRecyclerHolder;
import com.yyk.yiliao.util.Md5Util2;
import com.yyk.yiliao.util.rx.ApiService;
import com.yyk.yiliao.bean.OrderClosebankInfo;
import com.yyk.yiliao.util.rx.RxUtils;
import com.yyk.yiliao.widget.diver.CustomDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import butterknife.BindView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * 选择银行列表  合作银行
 */
public class SelectYinhangCardActivty extends BaseActivity {
    public static final int RESULT_OK = 2; //大于1


    @BindView(R.id.rl_card)
    RecyclerView rlCard;
    private List<OrderClosebankInfo.DataBean> cardList = new ArrayList<>();
    private BaseRecyclerAdapter<OrderClosebankInfo.DataBean> cardAdapter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_select_yinhang_card;
    }

    @Override
    public void initView() {
        initToolbar();
    }

    private void initToolbar() {
        setBackArrow();
        setTitle("合作银行");
    }

    @Override
    public void initData() {
        initUserbankBankcard(true);
        initAdapter();
    }

    private void initAdapter() {
        cardAdapter = new BaseRecyclerAdapter<OrderClosebankInfo.DataBean>(mActivity, cardList, R.layout.adapter_item_yinhangcardlist) {
            @Override
            public void convert(BaseRecyclerHolder holder, OrderClosebankInfo.DataBean item, int position, boolean isScrolling) {
                holder.setImageByUrl(R.id.iv_img, ApiService.BASE_URL + item.getImg());
                if (item.getDoc().equals("DC")){
                    holder.setText(R.id.tv_name, item.getName() + "(储蓄卡)");
                }else if (item.getDoc().equals("CC")){
                    holder.setText(R.id.tv_name, item.getName() + "(信用卡)");
                }else{
                    holder.setText(R.id.tv_name, item.getName() + "(储蓄卡、信用卡)");

                }

            }
        };
        rlCard.addItemDecoration(new CustomDecoration(mActivity, CustomDecoration.VERTICAL_LIST, R.drawable.divider_item_e6_05, 0));
        rlCard.setLayoutManager(new LinearLayoutManager(mActivity));
        rlCard.setAdapter(cardAdapter);

        cardAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(RecyclerView parent, View view, int position) {
                // 设置返回数据
//                Bundle bundle = new Bundle();
//                bundle.putString("name", cardList.get(position).getName() + "");
//                bundle.putInt("bankid", cardList.get(position).getId());
//                Intent intent = new Intent();
//                intent.putExtras(bundle);
//                setResult(RESULT_OK, intent);
//                finish();

            }
        });


    }


    private void initUserbankBankcard(boolean flag) {
        Map<String, String> map = new TreeMap<>();
        map.put("sign", Md5Util2.createSign(map));
        RxUtils.getInstance().retrofit.create(ApiService.class).postOrderClosebank(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())
                .subscribe(new Subscriber<OrderClosebankInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(OrderClosebankInfo info) {
                        Logger.e("合作银行" + info);
                        if (info.getCode() == 1) {
                            if (flag) {
                                cardList.clear();
                            }
                            if (info.getData() != null && info.getData().size() > 0) {
                                List<OrderClosebankInfo.DataBean> data = info.getData();
                                cardList.addAll(data);
                            }
                            cardAdapter.notifyDataSetChanged();
                        }
                    }
                });
    }


}
